package com.store.bookstore_backend.web.controller;

import com.store.bookstore_backend.domain.Address;
import com.store.bookstore_backend.dto.APIResponse;
import com.store.bookstore_backend.dto.AddressAddDTO;
import com.store.bookstore_backend.dto.AddressGetDTO;
import com.store.bookstore_backend.dto.AddressUpdateDTO;
import com.store.bookstore_backend.mapper.AddressMapper;
import com.store.bookstore_backend.mapper.UserMapper;
import com.store.bookstore_backend.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class AddressController {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    AddressService addressService;

    @GetMapping("/address")
    public APIResponse<List<AddressGetDTO>> getAddress(HttpServletRequest request
    ) {
        return APIResponse.success(addressService.getAddress(request));
    }

    @PostMapping("/address")
    public APIResponse<Object> createAddress(AddressAddDTO addressAddDTO) {
        addressService.createAddress(addressAddDTO);
        return APIResponse.success(null);
    }

    @DeleteMapping("/address")
    public APIResponse<Object> deleteAddress(int id) {
        addressService.deleteAddress(id);
        return APIResponse.success(null);
    }

    @PutMapping("/address")
    public APIResponse<Object> updateAddress(@RequestBody AddressUpdateDTO addressUpdateDTO) {
//        System.out.println(addressUpdateDTO);
        addressService.updateAddress(addressUpdateDTO);
        return APIResponse.success();
    }

    //设置默认收获地址接口
    @Autowired
    UserMapper userMapper;

    @PutMapping("/address/setDefault/{addressId}")
    public APIResponse<Object> setDefault(@PathVariable int addressId, HttpServletRequest request
    ) {
        addressService.setDefaultAddress(addressId, request);
        return APIResponse.success();
    }

    @PutMapping("/address/cancelDefault/{addressId}")
    public APIResponse<Object> cancelDefault(@PathVariable int addressId, HttpServletRequest request
    ) {
        addressService.cancelDefault(addressId, request);
        return APIResponse.success();
    }

}
